package com.zjq.smartdevice.controller;

import com.github.pagehelper.PageInfo;
import com.zjq.smartdevice.dto.R;
import com.zjq.smartdevice.pojo.SysCategory;
import com.zjq.smartdevice.service.SysCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @User zhangjiaqi
 * @Project project
 * @Package com.zjq.smartdevice.controller
 * @Date 2022/3/18 19:23
 */
@RestController
@RequestMapping("/categories")
public class CategoryController {

    private SysCategoryService categoryService;

    @Autowired
    public void setCategoryService(SysCategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @GetMapping("/category/{id}")
    public R findById(@PathVariable Long id) {
        SysCategory category = categoryService.findById(id);
        return R.setOK(category);
    }

    @PostMapping("/category")

    public R addSysCategory(@RequestBody SysCategory category) {
        categoryService.addSysCategory(category);
        return R.setOK();
    }

    @GetMapping("/categories")
    public R findAllPage(@RequestParam(defaultValue = "10",name = "page") int pageNum, @RequestParam(defaultValue = "1",name = "limit") int pageSize, String categoryName) {
        PageInfo<SysCategory> pageInfo = categoryService.findByPage(pageSize, pageNum, categoryName);
        return R.setOK(pageInfo);
    }

    @PostMapping("/category/edit")
    public R updateCategory(@RequestBody SysCategory category) {
        int count = categoryService.updateCategory(category);
        return R.setOK(count);
    }

    @PostMapping("/category/delete")
    public R deleteByIds(@RequestBody List<Long> ids) {
        int count = categoryService.deleteByIds(ids);
        return R.setOK(count);
    }
}
